package top.easyblog.core.converter;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import top.easyblog.common.bean.UrlMappingBean;
import top.easyblog.core.dao.auto.model.MappingVisitRecord;
import top.easyblog.core.dao.auto.model.UrlMapping;
import top.easyblog.core.mq.bean.MappingVisitMessage;
import top.easyblog.core.mq.bean.UrlMappingMessage;
import top.easyblog.core.support.context.UrlMappingBuildContext;

/**
 * @author: frank.huang
 * @date: 2023-12-18 22:47
 */
@Mapper(componentModel = "spring")
public interface BeanMapper {


    UrlMapping convertRequest2UrlMapping(UrlMappingBuildContext context);


    UrlMapping convertRequest2UrlMapping(UrlMappingMessage.UrlMappingMessageBean urlMappingMessageBean);


    @Mapping(target = "createTime", expression = "java(java.util.Optional.ofNullable(urlMapping.getCreateTime()).map(time->time.getTime()/1000).orElse(null))")
    @Mapping(target = "updateTime", expression = "java(java.util.Optional.ofNullable(urlMapping.getUpdateTime()).map(time->time.getTime()/1000).orElse(null))")
    UrlMappingBean convertUrlMapping2Bean(UrlMapping urlMapping);

    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    UrlMappingBean convertUrlMapping2Bean(UrlMappingBuildContext context);

    UrlMappingMessage.UrlMappingMessageBean convertUrlMapping2KfMessageBean(UrlMappingBuildContext context);

    @Mapping(target = "shortUrl", source = "tinyUrl")
    @Mapping(target = "longUrl", source = "sourceUrl")
    @Mapping(target = "deleted", expression = "java(false)")
    @Mapping(target = "shortUrlDigest", expression = "java(top.easyblog.core.util.EncryptUtils.SHA256(message.getTinyUrl()))")
    @Mapping(target = "longUrlDigest", expression = "java(top.easyblog.core.util.EncryptUtils.SHA256(message.getSourceUrl()))")
    MappingVisitRecord convertMappingVisitMessage2MappingVisitRecord(MappingVisitMessage message);
}
